package com.leyou.service;

import com.leyou.domian.SpecGroup;
import com.leyou.domian.SpecParam;
import com.leyou.domian.Spu;
import com.leyou.dto.SpecGroupDTO;
import com.leyou.dto.SpecParamDTO;
import com.leyou.exception.LyException;
import com.leyou.exception.domain.ExceptionEnum;
import com.leyou.mapper.SpecGroupMapper;
import com.leyou.mapper.SpecParmaMapper;
import com.leyou.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
public class SpecService {

    @Autowired
    SpecGroupMapper specGroupMapper;

    @Autowired
    SpecParmaMapper specParmaMapper;


    /**
     * 通过分类查询规格组信息
     *
     * @param cid 分类ID
     * @return 规格组
     */
    public List<SpecGroupDTO> findSpecGroupByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> specGroupList = specGroupMapper.select(specGroup);
        List<SpecGroupDTO> specGroupDTOList = BeanHelper.copyWithCollection(
                specGroupList, SpecGroupDTO.class);
        if (CollectionUtils.isEmpty(specGroupDTOList)) {
            throw new LyException(ExceptionEnum.DATA_TRANSFER_ERROR);
        }
        return specGroupDTOList;
    }

    /**
     * 通过规格组或者分类id来查询
     *
     * @param gid    规格组
     * @param cid    分类
     * @param search 是否
     * @return
     */
    public List<SpecParamDTO> findSpecParam(Long gid, Long cid, Boolean search) {
        if (gid == null && cid == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        SpecParam specParam = new SpecParam();
        specParam.setCid(cid);
        specParam.setGroupId(gid);
        specParam.setSearching(search);
        List<SpecParam> specParamList = specParmaMapper.select(specParam);
        List<SpecParamDTO> specParamDTOList = BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
        if (CollectionUtils.isEmpty(specParamDTOList)) {
            throw new LyException(ExceptionEnum.DATA_TRANSFER_ERROR);
        }

        return specParamDTOList;
    }


}
